message(NOTICE "开始构建 ssh 模块")
message(NOTICE "开始构建 模块")
# 检查构建目标是否存在
if(TARGET ssh-${SSH_TAG})
    message(NOTICE "ssh 模块已存在 ssh-${SSH_TAG}")
    return()
endif()
# 依赖库
include(${__DIR}/module/zlib_module.cmake)
include(${__DIR}/module/openssl_module.cmake)
# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/ssh/${SSH_TAG}
    ${MODULE_INSTALL_DIR}/ssh/${SSH_TAG}
    ssh-${SSH_TAG})
# 配置库参数成成规则
set(SSH_CONFIGURE CACHE STRING "ssh configure" FORCE)
# Android 配置 模块
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
    # 编译器工具链
    list(APPEND SSH_CONFIGURE "-DANDROID_ABI=${ANDROID_ABI}")
    list(APPEND SSH_CONFIGURE "-DANDROID_PLATFORM=${ANDROID_PLATFORM}")
    list(APPEND SSH_CONFIGURE "-DANDROID_STL=${ANDROID_STL}")
    list(APPEND SSH_CONFIGURE "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}")
    list(APPEND SSH_CONFIGURE "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
    list(APPEND SSH_CONFIGURE "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
    list(APPEND SSH_CONFIGURE "-DCMAKE_SYSROOT=${CMAKE_SYSROOT}")
    list(APPEND SSH_CONFIGURE "-DCMAKE_C_FLAGS=-DS_IWRITE=S_IWUSR")
    list(APPEND SSH_CONFIGURE "-DCMAKE_CXX_FLAGS=-DS_IWRITE=S_IWUSR")
endif()

# 构建 ssh 库
ExternalProject_Add(
    ssh-${SSH_TAG}
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/ssh/${SSH_TAG}          # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/ssh/${SSH_TAG}        # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/ssh/${SSH_TAG}  # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/ssh/${SSH_TAG}        # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/ssh/${SSH_TAG}          # ✅ 显式设置 tmp 目录

    DOWNLOAD_COMMAND # 下载命令
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${SSH_REPO}
            -DGIT_TAG=${SSH_TAG}
            -P ${__DIR}/utils/git.cmake
    CONFIGURE_COMMAND
        COMMAND ${CMAKE_COMMAND}
            -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
            -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON # 标准 C/C++ 编译选项
            -DWITH_SERVER=ON
            -DWITH_SFTP=ON
            -DWITH_ZLIB=ON
            -DBUILD_SHARED_LIBS=OFF
            -DWITH_GCRYPT=OFF
            -DWITH_MBEDTLS=OFF
            -DWITH_GSSAPI=OFF
            -DWITH_NACL=OFF
            -DCLIENT_TESTING=OFF #是否构建客户端测试（需要 OpenSSH
            -DSERVER_TESTING=OFF
            -DWITH_INTERNAL_DOC=OFF
            -DWITH_EXAMPLES=OFF
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -S <SOURCE_DIR>                      # ✅ 显式设置 source 目录
            -B <BINARY_DIR>                       # ✅ 显式设置 build 目录
            ${SSH_CONFIGURE}

    BUILD_COMMAND
        ${CMAKE_COMMAND}
        --build <BINARY_DIR>
        --config ${CMAKE_BUILD_TYPE}
        -j${NPROC}
    INSTALL_COMMAND
        ${CMAKE_COMMAND}
            --install <BINARY_DIR>
            --config ${CMAKE_BUILD_TYPE}
        # 将安装文件路径保存到 文件去
        COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     OFF                           # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志

    # ✅ 关键：声明依赖
#    DEPENDS zlib-${ZLIB_TAG} openssl-${OPENSSL_TAG}
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)

# ----------------------------
# 创建 IMPORTED 静态库
# ----------------------------
add_library(ssh::ssh STATIC IMPORTED GLOBAL)
# 属性配置
set_target_properties(ssh::ssh PROPERTIES
    IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
    IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/ssh/${SSH_TAG}/lib/libssh.a
    INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/ssh/${SSH_TAG}/include
    INTERFACE_LINK_LIBRARIES "ZLIB::ZLIBSTATIC;OpenSSL::Crypto"
)

#依赖库
add_dependencies(ssh-${SSH_TAG} zlib-${ZLIB_TAG} openssl-${OPENSSL_TAG})
# ✅ 正确做法：让 IMPORTED 库依赖 ExternalProject 构建目标
add_dependencies(ssh::ssh ssh-${SSH_TAG})











